package com.cqust.junit;

import com.cqust.dao.CardDao;
import com.cqust.dao.StudentDao;
import com.cqust.entity.Card;
import com.cqust.entity.Student;
import com.cqust.service.CardService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class MyUnitTest {

//    @Test
    public void testUpdate()
    {
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jdbcTemplate= (JdbcTemplate) ac.getBean("jdbcTemplate");
        String sql="update student set name='彭竹庚' where sno='2020444229'";
        jdbcTemplate.execute(sql);
    }

    //@Test
    public void testInsert()
    {
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jdbcTemplate= (JdbcTemplate) ac.getBean("jdbcTemplate");
        String sql="insert into student(sno,name,age,major) values ('2020444229','彭竹庚',22,'软件工程')";
//        String sql="update student set name='彭竹庚' where sno='2020444229'";
        jdbcTemplate.execute(sql);
    }

    //@Test
    public void testStudntInsert()
    {
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentDao sdao= (StudentDao) ac.getBean("studentDao");
        Student s=new Student();
        s.setAge(22);
        s.setName("真纪真");
        s.setSno("002");
        s.setMajor("软件工程");
        int flage=sdao.insert(s);
        if(flage==1)
        {
            System.out.println("成功插入一条数据!");
        }
    }

    //@Test
    public void studentUpdate()
    {
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentDao sdao= (StudentDao) ac.getBean("studentDao");
        Student s=new Student();
        s.setSid(1);
        s.setAge(20);
        int flage=sdao.updateById(s);
        if(flage==1)
        {
            System.out.println("成功修改一条数据!");
        }
    }

    //@Test
    public void studentDelete()
    {
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentDao sdao= (StudentDao) ac.getBean("studentDao");
        int flage=sdao.deleteById(3);
        if(flage==1)
        {
            System.out.println("成功删除一条数据!");
        }
    }
    //@Test
    public void selectById()
    {
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentDao sdao= (StudentDao) ac.getBean("studentDao");
        System.out.println(sdao.selectById(2));
    }

    //@Test
    public void selectByCondition()
    {
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentDao sdao= (StudentDao) ac.getBean("studentDao");
        List<Student> s=sdao.selectByCondition("name='彭竹庚'");
        for(int i=0;i<s.size();i++)
            System.out.println(s.get(i));
    }

    //@Test
    public void cardInsert()
    {
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        CardDao cardDao= (CardDao) ac.getBean("cardDao");
        Card card=new Card();
        card.setSid(1);
        card.setSno("001");
        card.setName("呜喵王");
        card.setCno("S101");
        card.setBalance(1000);
        card.setLastUseTime("2022-10-20");
        int flage=cardDao.insert(card);
        if(flage==1)
        {
            System.out.println("成功插入一条数据!");
        }
    }

    //@Test
    public void cardService()
    {
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        CardService cardService= (CardService) ac.getBean("cardService");
        int flage=cardService.openAccount("2020444229","彭竹庚","S101",1000);
        if(flage>0)
        {
            System.out.println("成功插入一条数据！");
        }
        else
            System.out.println("该学生不存在！");
    }
}
